package cn.dakaqi.vo.response;

import cn.dakaqi.entities.ActivityRecruit;
import cn.dakaqi.entities.GroupRecruit;
import cn.dakaqi.entities.IndexData;
import cn.dakaqi.entities.SysTopic;
import cn.dakaqi.utils.DateUtil;
import com.fasterxml.jackson.annotation.JsonFormat;
import lombok.Data;
import lombok.ToString;

import java.io.Serializable;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * Copyright: Copyright (c)2016
 * Company: 志愿者打卡器(www.dakaqi.cn)
 * 类名称: IndexDataVO <br>
 * 类描述: <br>
 *
 * @author: ChunYang.Zhang
 * @since: 2016/9/18 17:01
 * @version: 1.0.0
 */
@ToString
public class IndexDataVO implements Serializable
{

    private static final long serialVersionUID = -8749559022982096337L;
    private Long id;
    private String type;    //分类,
    private int delStatus;//是否被删除
    private int top;//是否被置顶
    private Date createTime;//创建日期
    private int display;//是否显示
    private String city;    //招募的地点
    private String job;    //招募所需的职业
    private String serviceField;    //招募的服务领域,
    private String skill;    //招募所需的个人技能
    private Date stopTime;
    private int stopStatus;

    private String img;     //封面图
    private String title;   //标题
    private Long objectId;    //对象类型ID
    private String code;//活动的一号或社团的一号通
    private int browses;//浏览量
    private int participants;//参与人数
    private String nickName;//话题发布者昵称
    private int status;//进行状态
    private long days;//招募剩余的天数
    private String intro;//话题简介
    private String orgName;//组织名称
    private String content;//话题内容


    public static IndexDataVO buildVO(IndexData indexData)
    {
        IndexDataVO indexDataVO = new IndexDataVO();
        indexDataVO.setId(indexData.getId());
        indexDataVO.setType(indexData.getType());
        indexDataVO.setDelStatus(indexData.getDelStatus());
        indexDataVO.setTop(indexData.getTop());
        indexDataVO.setCreateTime(indexData.getCreateTime());
        indexDataVO.setDisplay(indexData.getDisplay());
        indexDataVO.setCity(indexData.getCity());
        indexDataVO.setJob(indexData.getJob());
        indexDataVO.setServiceField(indexData.getServiceField());
        indexDataVO.setSkill(indexData.getSkill());
        indexData.setStopTime(indexData.getStopTime());
        indexDataVO.setStopStatus(indexData.getStopStatus());

        indexDataVO.setImg(getImg(indexData));
        indexDataVO.setTitle(getTitle(indexData));
        indexDataVO.setCode(getCode(indexData));
        indexDataVO.setObjectId(getObjectId(indexData));
        indexDataVO.setBrowses(getBrowses(indexData));
        indexDataVO.setParticipants(getParticipants(indexData));
        indexDataVO.setNickName(getNickName(indexData));
        indexDataVO.setStopStatus(getStatus(indexData));
        indexDataVO.setDays(getDays(indexData));
        indexDataVO.setIntro(getIntro(indexData));
        indexDataVO.setOrgName(getOrgName(indexData));
        indexDataVO.setContent(getContent(indexData));
        return indexDataVO;
    }


    public static String getCode(IndexData indexData)
    {
        String code = "",type = indexData.getType();
        if(type.equals("GroupRecruit")&& null != indexData.getGroupRecruit())
        {
            code = indexData.getGroupRecruit().getGroup().getGroupCode();
        }
        else if(type.equals("ActivityRecruit")&& null != indexData.getActivityRecruit())
        {
            code =  indexData.getActivityRecruit().getActivity().getActivityCode();
        }
        return code;
    }

    public static int getStatus(IndexData indexData)
    {
        String type = indexData.getType();
        int status = 0;

        if(type.equals("GroupRecruit")&& null != indexData.getGroupRecruit())
        {
            status = indexData.getGroupRecruit().getStatus();
        }
        else if(type.equals("ActivityRecruit")&& null != indexData.getActivityRecruit())
        {
            status =  indexData.getActivityRecruit().getStatus();
        }
        return status;
    }

    public static String getIntro(IndexData indexData)
    {
        String intro = "";
        String type = indexData.getType();
        if(type.equals("SysTopic") && null != indexData.getSysTopic())
        {
            intro = indexData.getSysTopic().getIntro();
        }
        else
            intro = "";

        return intro;
    }
    public static String getContent(IndexData indexData)
    {
        String content = "";
        String type = indexData.getType();
        if(type.equals("SysTopic") && null != indexData.getSysTopic())
        {
            content = indexData.getSysTopic().getContent();
        }
        else
            content = "";

        return content;
    }
    public static String getOrgName(IndexData indexData)
    {
        String orgName = "";
        String type = indexData.getType();
        if(type.equals("GroupRecruit")&& null != indexData.getGroupRecruit())
            orgName = indexData.getGroupRecruit().getGroup().getShortName();
        else if(type.equals("ActivityRecruit")&& null != indexData.getActivityRecruit())
            orgName = indexData.getActivityRecruit().getActivity().getGroup().getShortName();

        return orgName;
    }

    private static long calcDays(String startDay,String endDay)
    {
        long days = DateUtil.countDay(startDay, endDay) + 1;
        return days;
    }
    public static long getDays(IndexData indexData)
    {
        if(indexData.getStopStatus() >1)
            return -1;

        String type = indexData.getType();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        try
        {
            if(type.equals("GroupRecruit") && null != indexData.getGroupRecruit())
            {
                return calcDays(sdf.format(new Date()),sdf.format(indexData.getStopTime()));
            }
            else if(type.equals("ActivityRecruit") && null != indexData.getActivityRecruit())
            {
                return calcDays(sdf.format(new Date()),sdf.format(indexData.getStopTime()));
            }
        } catch (Exception e)
        {
            e.printStackTrace();
        }
        return -1;
    }
    public static String getNickName(IndexData indexData)
    {
        String nickName = "";
        String type = indexData.getType();
        if(type.equals("SysTopic")&& null != indexData.getSysTopic())
            nickName = indexData.getSysTopic().getPromulgator();
        return nickName;
    }
    public static int getBrowses(IndexData indexData)
    {
        int browses = 0;
        String type = indexData.getType();
        if(type.equals("SysTopic")&& null != indexData.getSysTopic())
            browses = indexData.getSysTopic().getBrowses();
        if(type.equals("GroupRecruit")&& null != indexData.getGroupRecruit())
            browses = indexData.getGroupRecruit().getBrowses();
        if(type.equals("ActivityRecruit")&& null != indexData.getActivityRecruit())
            browses = indexData.getActivityRecruit().getActivity().getBrowses();

        return browses;
    }
    public static int getParticipants(IndexData indexData)
    {
        int participants = 0;
        String type = indexData.getType();
        if(type.equals("SysTopic")&& null != indexData.getSysTopic())
            participants = indexData.getSysTopic().getParticipants();
        else if(type.equals("GroupRecruit")&& null != indexData.getGroupRecruit())
            participants = indexData.getGroupRecruit().getApplys();
        else if(type.equals("ActivityRecruit")&& null != indexData.getActivityRecruit())
            participants = indexData.getActivityRecruit().getActivity().getApplys();
        return participants;
    }
    public static String getImg(IndexData indexData)
    {
        String type = indexData.getType();
        String img = "";
        if(type.equals("SysTopic")&& null != indexData.getSysTopic())
            img = indexData.getSysTopic().getBackground();
        else if(type.equals("GroupRecruit")&& null != indexData.getGroupRecruit())
            img = indexData.getGroupRecruit().getImg();
        else if(type.equals("ActivityRecruit")&& null != indexData.getActivityRecruit())
            img = indexData.getActivityRecruit().getImg();
        return img;
    }
    public static String getTitle(IndexData indexData)
    {
        String title = "";

        String type = indexData.getType();
        if(type.equals("SysTopic") && null != indexData.getSysTopic())
        {
            title = indexData.getSysTopic().getTitle();
        }
        else if(type.equals("GroupRecruit") && null != indexData.getGroupRecruit())
            title = indexData.getGroupRecruit().getTitle();
        else if(type.equals("ActivityRecruit") && null != indexData.getActivityRecruit())
            title = indexData.getActivityRecruit().getActivity().getName();
        return title;
    }
    public static Long getObjectId(IndexData indexData)
    {
        Long objectId = 0L;
        String type = indexData.getType();
        if(type.equals("SysTopic")&& null != indexData.getSysTopic())
            objectId = indexData.getSysTopic().getId();
        else if(type.equals("GroupRecruit")&& null != indexData.getGroupRecruit())
            objectId = indexData.getGroupRecruit().getId();
        else if(type.equals("ActivityRecruit")&& null != indexData.getActivityRecruit())
            objectId = indexData.getActivityRecruit().getId();
        return objectId;
    }

    public Long getId()
    {
        return id;
    }

    public void setId(Long id)
    {
        this.id = id;
    }

    public String getType()
    {
        return type;
    }

    public void setType(String type)
    {
        this.type = type;
    }

    public int getDelStatus()
    {
        return delStatus;
    }

    public void setDelStatus(int delStatus)
    {
        this.delStatus = delStatus;
    }

    public int getTop()
    {
        return top;
    }

    public void setTop(int top)
    {
        this.top = top;
    }

    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+08:00")
    public Date getCreateTime()
    {
        return createTime;
    }

    public void setCreateTime(Date createTime)
    {
        this.createTime = createTime;
    }

    public int getDisplay()
    {
        return display;
    }

    public void setDisplay(int display)
    {
        this.display = display;
    }

    public String getCity()
    {
        return city;
    }

    public void setCity(String city)
    {
        this.city = city;
    }

    public String getJob()
    {
        return job;
    }

    public void setJob(String job)
    {
        this.job = job;
    }

    public String getServiceField()
    {
        return serviceField;
    }

    public void setServiceField(String serviceField)
    {
        this.serviceField = serviceField;
    }

    public String getSkill()
    {
        return skill;
    }

    public void setSkill(String skill)
    {
        this.skill = skill;
    }

    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+08:00")
    public Date getStopTime()
    {
        return stopTime;
    }

    public void setStopTime(Date stopTime)
    {
        this.stopTime = stopTime;
    }

    public int getStopStatus()
    {
        return stopStatus;
    }

    public void setStopStatus(int stopStatus)
    {
        this.stopStatus = stopStatus;
    }

    public String getImg()
    {
        return img;
    }

    public void setImg(String img)
    {
        this.img = img;
    }

    public String getTitle()
    {
        return title;
    }

    public void setTitle(String title)
    {
        this.title = title;
    }

    public Long getObjectId()
    {
        return objectId;
    }

    public void setObjectId(Long objectId)
    {
        this.objectId = objectId;
    }

    public String getCode()
    {
        return code;
    }

    public void setCode(String code)
    {
        this.code = code;
    }

    public int getBrowses()
    {
        return browses;
    }

    public void setBrowses(int browses)
    {
        this.browses = browses;
    }

    public int getParticipants()
    {
        return participants;
    }

    public void setParticipants(int participants)
    {
        this.participants = participants;
    }

    public String getNickName()
    {
        return nickName;
    }

    public void setNickName(String nickName)
    {
        this.nickName = nickName;
    }

    public int getStatus()
    {
        return status;
    }

    public void setStatus(int status)
    {
        this.status = status;
    }

    public long getDays()
    {
        return days;
    }

    public void setDays(long days)
    {
        this.days = days;
    }

    public String getIntro()
    {
        return intro;
    }

    public void setIntro(String intro)
    {
        this.intro = intro;
    }

    public String getOrgName()
    {
        return orgName;
    }

    public void setOrgName(String orgName)
    {
        this.orgName = orgName;
    }

    public String getContent()
    {
        return content;
    }

    public void setContent(String content)
    {
        this.content = content;
    }
}
